package top.jnaw.jee.utils.jpush;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Map;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.model.MessageCenter;
import top.jnaw.jee.platform.model.Users;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Shiros;
import top.jnaw.jee.utils.Strings;

/**
 * Copyright (C) 2018 版权所有。 <p> 文件名： 文件功能描述：极光推送
 *
 * @author lc  2018/6/25 11:48 创建内容： <p> <p> 修改标识： 修改原因：
 */
public class JPushs {

  /**
   * 发送极光推送
   */
  public static void sendjpush(boolean local, String title, String alert,
      List<Users> pushUsers, Map<String, String> extras) {
    //发送极光推送
    if (null != pushUsers && pushUsers.size() > 0) {
      int senderUid = 1;
      Users u = Users.dao.findById(senderUid);
      String username = u.getUsername();
      if (!local) {
        username = Shiros.getUser();
        if (null != username) {
          u = Users.getByUsername(username);
          if (null != u && u.getId() > 0) {
            senderUid = u.getId();
          }
        }
      }
      for (Users users : pushUsers) {
        ModelResult result = MessageCenter
            .add(senderUid, users.getUsername(), alert, title, extras);
        Log.e("message_center add result::" + result.toJson());
      }
      pushUsers = Strings.search(username, pushUsers);
      Log.e("pushUsers::" + pushUsers.toString());
      for (Users user : pushUsers) {
        sendPush(user.getUsername(), alert, title, extras);
      }
    }
  }

  public static void sendjpush(String title, String alert,
      List<Users> pushUsers, Map<String, String> extras) {
    sendjpush(false, title, alert, pushUsers, extras);
  }

  public static void sendPush(String alias, String alert, String title,
      Map<String, String> extras) {

    JPushClient jpushClient = new JPushClient(
        Consts.JPUSH_MASTER_SECRET,
        Consts.JPUSH_APP_KEY, null,
        ClientConfig.getInstance());

    PushPayload payload = PushPayload.newBuilder()
        .setPlatform(Platform.all())
        .setAudience(Audience.alias(alias))
        .setNotification(Notification.android(alert, title, extras))
        .build();
    JSONObject resultJson = null;
    try {
      PushResult result = jpushClient.sendPush(payload);
      Log.w("Got result -::: " + result);
      resultJson = Strings.toJSONObject(result.toString());
      Log.w("Got result - " + resultJson);
    } catch (APIConnectionException e) {
      // Connection error, should retry later
      Log.e("Connection error, should retry later");
//      e.printStackTrace();

    } catch (APIRequestException e) {
      // Should review the error, and fix the request
      Log.e("Should review the error, and fix the request");
//      e.printStackTrace();
      Log.w("HTTP Status: " + e.getStatus());
      Log.w("Error Code: " + e.getErrorCode());
      Log.w("Error Message: " + e.getErrorMessage());
      Log.w("Error MessageId: " + e.getMsgId());
    }
  }
}
